package cc.fs.zhihuapp.ui.activity.demo;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cc.fs.zhihuapp.R;
import cc.fs.zhihuapp.ui.activity.helper.BaseActivity;
import cc.fs.zhihuapp.utils.MyExoPlayer;
import cc.fs.zhihuapp.utils.TimeUtil;

/**
 * Created by fostion on 2015/10/30.
 */
public class MyVideoActivity3 extends BaseActivity {

    @InjectView(R.id.surfaceView)
    SurfaceView surfaceView;
    @InjectView(R.id.progressDialog)
    RelativeLayout progressDialog;
    @InjectView(R.id.backBtn)
    ImageView backBtn;
    @InjectView(R.id.playBtn)
    ImageView playBtn;
    @InjectView(R.id.forwardBtn)
    ImageView forwardBtn;
    @InjectView(R.id.bottomTab)
    LinearLayout bottomTab;
    @InjectView(R.id.progressbar)
    SeekBar progressbar;
    @InjectView(R.id.time)
    TextView time;
    @InjectView(R.id.bottomLayout)
    RelativeLayout bottomLayout;
    @InjectView(R.id.toolbar)
    Toolbar toolbar;
    @InjectView(R.id.speed)
    TextView speed;

//    String url = "http://ssports.vod.smgbb.cn.cloudcdn.net/otv/vod/auto_bbf0913484a59baedb5d694236a45cc2.m3u8";
    MyExoPlayer player;
//    String url = "https://d33e9z4l6nr9ho.cloudfront.net/hls/1dia/13fb7fa0-370d-11e5-9a60-35a00edea567.stream/index.m3u8";
    String url = "http://mobilelive1-cnc.wasu.cn/zjws/z.m3u8";
//    String url = "http://ips.ifeng.com/video19.ifeng.com/video09/2014/06/16/1989823-102-086-0009.mp4";
//    String url = "http://qiubai-video.qiushibaike.com/MRI2HAG8T5FGJGE3.mp4";


    SurfaceHolder surfaceHolder;

    Animation outBottomAnimation;
    Animation inBottomAnimation;
    Animation outTopAnimation;
    Animation inTopAnimation;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_video);
        ButterKnife.inject(this);

        toolbar.setNavigationIcon(R.drawable.ic_back);
        toolbar.setTitle("电影播放器");
        setSupportActionBar(toolbar);

        player = new MyExoPlayer(this);
        player.setPlayerListener(new MyExoPlayer.PlayerListener() {
            @Override
            public void onPrepared() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressDialog.setVisibility(View.GONE);
                    }
                });
            }

            @Override
            public void onBuffering() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressDialog.setVisibility(View.VISIBLE);
                    }
                });
            }

            @Override
            public void onBuffered(final int buffered) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressbar.setSecondaryProgress(buffered);
                    }
                });

            }

            @Override
            public void onProgress(final int total, final int progress) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressbar.setMax(total);
                        progressbar.setProgress(progress);
                        time.setText(TimeUtil.formatVideoTime(progressbar.getProgress()) + "/" + TimeUtil.formatVideoTime(progressbar.getMax()));
                    }
                });

            }

            @Override
            public void onCompletion() {

            }
        });
        init();
    }

    void init(){
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        outBottomAnimation = AnimationUtils.loadAnimation(this, R.anim.bottom_play_out);
        inBottomAnimation = AnimationUtils.loadAnimation(this, R.anim.bottom_play_in);
        outTopAnimation = AnimationUtils.loadAnimation(this, R.anim.top_play_out);
        inTopAnimation = AnimationUtils.loadAnimation(this, R.anim.top_play_in);
        speed.setVisibility(View.GONE);
        progressDialog.setVisibility(View.GONE);

        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.setFixedSize(480, 720);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                progressDialog.setVisibility(View.VISIBLE);
                playBtn.setImageResource(R.drawable.icon_pause);
                player.playUrl(url,surfaceHolder);
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {

            }
        });

        inBottomAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                bottomLayout.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        outBottomAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                bottomLayout.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        outTopAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                toolbar.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        inTopAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                toolbar.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        progressbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                player.pause();
                runDelay(new Runnable() {
                    @Override
                    public void run() {
                        player.seekTo(progressbar.getProgress());
                        player.start();
                    }
                }, 50);
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        surfaceView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bottomLayout.getVisibility() == View.VISIBLE) {
                    bottomLayout.startAnimation(outBottomAnimation);
                } else {
                    bottomLayout.startAnimation(inBottomAnimation);
                }

                if (toolbar.getVisibility() == View.VISIBLE) {
                    toolbar.startAnimation(outTopAnimation);
                } else {
                    toolbar.startAnimation(inTopAnimation);
                }
            }
        });


        playBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(player.isPlaying()){
                    player.pause();
                    playBtn.setImageResource(R.drawable.icon_play);
                } else {
                    player.start();
                    playBtn.setImageResource(R.drawable.icon_pause);
                }

            }
        });

        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player.seekTo(progressbar.getProgress() - 20000);
            }
        });

        forwardBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player.seekTo(progressbar.getProgress() + 20000);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void handler(Message msg) {
    }

    @Override
    protected void onStop() {
        if(player != null)
            player.release();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        if(player != null)
            player.release();
        super.onDestroy();
    }
}
